package com.mycat.chat.two.ui.friend;

import com.mycat.chat.two.ui.ChatRoom;
import com.mycat.chat.two.ui.ChatRoomPanel;
import com.mycat.chat.two.util.PictureUtil;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 好友列表面板
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-21
 */
public class FriendPanel extends JPanel {
    private JTree jTree;
    private JScrollPane jScrollPane;
    private static Color inColor = new Color(254, 224, 109);
    private static Color selectColor = new Color(249, 184, 87);

    public FriendPanel() {
        setLayout(new BorderLayout());

        jTree = new JTree(initData());
        jTree.setUI(new BasicTreeUI()); // 自定义UI
        jTree.setCellRenderer(new FriendNodeRenderer());// 自定义节点渲染器
        jTree.setRootVisible(false);// 隐藏根节点

        jScrollPane = new JScrollPane();
        jScrollPane.setBorder(null);
        jScrollPane.setViewportView(jTree);
        jScrollPane.getVerticalScrollBar().setUI(new BasicScrollBarUI());
        jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        jTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ChatRoom room = new ChatRoom();
                ChatRoomPanel pane = new ChatRoomPanel();
                room.titleLabel.setText("测试" + " - " + "闹着玩");
                room.tabbedPane.addTab("闹着玩", null, pane, "闹着玩111");

                System.out.println("打开聊天窗口.........");
            }
        });

        this.add(jScrollPane, BorderLayout.CENTER);
    }

    public DefaultTreeModel initData() {
        final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        final DefaultTreeModel defaultTreeModel = new DefaultTreeModel(root);

        FriendNode node0 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 0, "左佐...");
        FriendNode node1 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 1, "左佐...");
        FriendNode node2 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 2, "左佐...");
        FriendNode node3 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 3, "左佐...");
        FriendNode node4 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 4, "左佐...");
        FriendNode node5 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 5, "左佐...");
        FriendNode node6 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 6, "左佐...");
        FriendNode node7 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 7, "左佐...");
        FriendNode node8 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 8, "左佐...");
        FriendNode node9 = new FriendNode(PictureUtil.getPicture("/main/avatar.png"), "左佐" + 9, "左佐...");

        root.add(node0);
        root.add(node1);
        root.add(node2);
        root.add(node3);
        root.add(node4);
        root.add(node5);
        root.add(node6);
        root.add(node7);
        root.add(node8);
        root.add(node9);
        return defaultTreeModel;
    }

}
